package com.lbssoft.gpsbd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbssoft.gpsbd.dao.EventMapper;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.EventVo;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.EventService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nczc on 2018/4/27.
 */
@Service
public class EventServiceImpl implements EventService {
    @Autowired
    public EventMapper eventMapper;

    @Override
    public Result getEvents(int page,int pageSize) {
        UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        PageHelper.startPage(page, pageSize);
        List<EventVo> events = eventMapper.getEvents(user.getId());

        Result result = new Result();
        PageInfo pageInfo = new PageInfo(events);
        Page pages = (Page) events;
        Map data = new HashMap();
        data.put("total", pageInfo.getTotal());
        data.put("events", pages);
        data.put("page", page);

        result.setCode(0);
        result.setData(data);
        return result;
    }

    @Override
    public Result readEvent(long id) {
        eventMapper.readEvent(id);
        return  new Result();
    }

    @Override
    public Result delete(long id) {
        eventMapper.delete(id);
        return new Result();
    }
}
